// pages/editaddress/editaddress.js
import { editaddress,deladdress } from "../../http/http";

Page({

  /**
   * 页面的初始数据
   */
  data: {
    data: [],
    show: false,
    showa: false,
    showb: false,
    shi: [],
    shia: "",
    shenga: "",
    sheng: [],
    xian: [],
    xiana: "",
    all: [],
    name: "",
    phone: "",
    adress: "",
    sfid: 1,
    csid: 1,
    qxid: 1,
    editid:1
  },
  // 姓名输入框输入
  change(e) {
    console.log(e.detail)
    this.setData({
      name: e.detail
    })
  },
  // 手机输入框输入
  changea(e) {
    console.log(e.detail)
    this.setData({
      phone: e.detail
    })
  },
  // 姓名输入框输入
  changeb(e) {
    console.log(e.detail)
    this.setData({
      adress: e.detail
    })
  },
  // 省份的弹出框
  gai() {
    this.setData({
      show: true
    })
  },
  // 市区的弹出框
  gaia() {
    this.setData({
      showa: true
    })
  },
  // 区县的弹出框
  gaib() {
    this.setData({
      showb: true
    })
  },
  // 省份确认
  onConfirm(e) {
    this.setData({
      shenga: e.detail.value,
      show: false
    })
    let id = this.data.all[e.detail.index].id
    this.setData({
      sfid: id
    })
    wx.request({
      url: `https://api.it120.cc/common/region/v2/child?pid=${id}`,
      success: res => {
        var a = []
        res.data.data.filter(item => {
          a.push(item.name)
        })
        this.setData({
          shi: a,
          all: res.data.data
        })
      }
    })
  },
  // 市区确认
  onConfirma(e) {
    this.setData({
      shia: e.detail.value,
      showa: false
    })
    let id = this.data.all[e.detail.index].id
    this.setData({
      csid: id
    })
    wx.request({
      url: `https://api.it120.cc/common/region/v2/child?pid=${id}`,
      success: res => {
        var a = []
        res.data.data.filter(item => {
          a.push(item.name)
        })
        this.setData({
          xian: a,
        })
      }
    })
  },
  onConfirmb(e) {
    console.log(this.data.all[e.detail.index].id)
    this.setData({
      qxid: this.data.all[e.detail.index].id
    })
    this.setData({
      xiana: e.detail.value,
      showb: false
    })
  },
  // 修改地址
  edit(){
    let obj ={
      token:wx.getStorageSync('token').token,
      address:this.data.adress,
      cityId:this.data.csid,
      linkMan:this.data.name,
      mobile:this.data.phone,
      provinceId:this.data.sfid,
      id:this.data.editid,
    }
    // 修改地址
    editaddress(obj).then(res =>{
      wx.showToast({
        title: "修改成功",
        icon: 'success',
        duration: 1000
      })
    })
  },
  // 删除地址
  del(){
    let obj ={
      token:wx.getStorageSync('token').token,
      id:this.data.editid,
    }
    deladdress(obj).then(res=>{
      console.log(res)
      wx.showToast({
        title: "删除成功",
        icon: 'success',
        duration: 1000
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(JSON.parse(options.item))
    this.setData({
      data: JSON.parse(options.item),
      shenga:JSON.parse(options.item).provinceStr,
      shia:JSON.parse(options.item).cityStr,
      sfid:JSON.parse(options.item).provinceId,
      csid:JSON.parse(options.item).cityId,
      name:JSON.parse(options.item).linkMan,
      phone:JSON.parse(options.item).mobile,
      adress:JSON.parse(options.item).address,
      editid:JSON.parse(options.item).id,
    })
    wx.request({
      url: 'https://api.it120.cc/common/region/v2/province',
      success: res => {
        var a = []
        res.data.data.filter(item => {
          a.push(item.name)
        })
        this.setData({
          sheng: a,
          all: res.data.data
        })

      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})